package com.pureut.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.production.domain.OperationInformation;
import com.pureut.production.domain.OperationProcessCenter;
import com.pureut.production.domain.dto.OperationInformationDto;
import com.pureut.production.domain.vo.OperationInformationVo;
import com.pureut.production.mapper.OperationInformationMapper;
import com.pureut.production.mapper.OperationProcessCenterMapper;
import com.pureut.production.service.OperationInformationService;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: YSL
 * @Date: 2023/2/6 15:11
 * @Desc: 工序信息 服务层
 */
@Service
public class OperationInformationServiceImpl extends ServiceImpl<OperationInformationMapper, OperationInformation> implements OperationInformationService {

    /**
     * 工序信息mapper
     */
    @Resource
    OperationInformationMapper operationInformationMapper;

    /**
     * 工艺流程明细
     */
    @Resource
    OperationProcessCenterMapper operationProcessCenterMapper;

    /**
     * feginService
     */
    @Resource
    FeignService feignService;

    /**
     * 查询工序信息列表
     * @param operationInformationVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<OperationInformationDto> selectOperationList(OperationInformationVo operationInformationVo) {

        List<OperationInformationDto> operationInformationDtos = operationInformationMapper.selectOperationList(operationInformationVo);
        //获取单据状态字典数据
        List<SysDictData> statusArray = DictUtils.getDictCache("quality_production_level");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        for (OperationInformationDto operationInformationDto : operationInformationDtos){
            // 生产阶别
            operationInformationDto.setProductionLevelDict(statusMap.get(String.valueOf(operationInformationDto.getProductionLevel())));
        }
        return operationInformationDtos;
    }

    /**
     * 根据工序id查询详情
     * @param id
     * @return
     */
    @Override
    public OperationInformationDto selectOperationByIds(Long id) {

        //获取单据状态字典数据
        List<SysDictData> statusArray = DictUtils.getDictCache("quality_production_level");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        OperationInformationDto operationInformationDto = operationInformationMapper.operationByIds(id);
        operationInformationDto.setProductionLevelDict(statusMap.get(String.valueOf(operationInformationDto.getProductionLevel())));
        return operationInformationDto;
    }

    /**
     * 新增工序信息
     * @param operationInformationVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(OperationInformationVo operationInformationVo) throws Exception{

        OperationInformation operationInformation = new OperationInformation();
        BeanUtils.copyProperties(operationInformationVo, operationInformation);
        // 工序代码
        operationInformation.setOperationCode(feignService.splitList("GX"));
        // 工序名称
        operationInformation.setProcessName(operationInformationVo.getProcessName());
        // 生产阶别
        operationInformation.setProductionLevel(operationInformationVo.getProductionLevel());
        // 工序内容
        operationInformation.setProcessContent(operationInformationVo.getProcessContent());
        operationInformation.setDeptId(SecurityUtils.getDeptId());
        operationInformation.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        operationInformation.setCreateTime(new Date());
        return save(operationInformation);
    }

    /**
     * 修改工序信息
     * @param operationInformationVo
     * @return
     */
    @Override
    public boolean edit(OperationInformationVo operationInformationVo) {
        // 新增工序信息
        OperationInformation operationInformation = operationInformationMapper.selectById(operationInformationVo.getId());

        // 工序名称
        operationInformation.setProcessName(operationInformationVo.getProcessName());
        // 生产阶别
        operationInformation.setProductionLevel(operationInformationVo.getProductionLevel());
        // 工序内容
        operationInformation.setProcessContent(operationInformationVo.getProcessContent());
        operationInformation.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        operationInformation.setUpdateTime(new Date());
        return updateById(operationInformation);
    }

    /**
     * 删除工序信息
     * @param idStr
     * @return
     */
    @Override
    public boolean deleteOperationInformationById(String idStr) {
        List<String> idList = Arrays.asList(idStr.split(","));
        if (idList.size() == 0) {
            throw new GlobalException("请至少勾选一条数据");
        }
        // TODO 如果工序存在关联流程工序 删除失败
        for (String id : idList){
            // 查询关系信息是否关联流程工序信息
            OperationProcessCenter operationProcessCenter = operationProcessCenterMapper.selectOne(new QueryWrapper<OperationProcessCenter>().lambda().eq(OperationProcessCenter::getInformationId, id));
           if (operationProcessCenter != null){
               OperationInformation operationInformation = operationInformationMapper.selectOne(new QueryWrapper<OperationInformation>().lambda().eq(OperationInformation::getId, operationProcessCenter.getInformationId()));
               throw new GlobalException("该工序名称：" + operationInformation.getProcessName() + "已关联工艺流程,不可删除");
            }
        }
        // 删除工序信息
        return removeByIds(idList);
    }
}
